package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.ProcessGroup;
import com.mxpio.erp.common.technology.entity.ProcessGroupLine;
import com.mxpio.erp.common.technology.service.ProcessGroupLineService;
import com.mxpio.erp.common.technology.service.ProcessGroupService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProcessGroupController", description = "工序段接口")
@RestController
@RequestMapping("/erp/tech/procgroup/")
public class ProcessGroupController {

    @Autowired
    private ProcessGroupService service;

    @Autowired
    private ProcessGroupLineService lineService;

    @GetMapping("list")
    @Operation(summary = "工序段列表", description = "获取工序段列表（无分页）", method = "GET")
    public Result<List<ProcessGroup>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProcessGroup> list = service.list(ProcessGroup.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "工序段列表", description = "获取工序段列表（分页）", method = "GET")
    public Result<Page<ProcessGroup>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProcessGroup> listPage = service.listPage(ProcessGroup.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{procGroupCode}")
    @Operation(summary = "根据procGroupCode获取工序段", description = "根据procGroupCode获取工序段", method = "GET")
    public Result<ProcessGroup> getById(@Parameter(description="通知单号") @PathVariable(name = "procGroupCode", required = true) String procGroupCode) {
        ProcessGroup data = service.getById(ProcessGroup.class, procGroupCode);
        return Result.OK(data);
    }

    @DeleteMapping("remove/{procGroupCodes}")
    @Operation(summary = "根据procGroupCode删除工序段", description = "删除工序段", method = "DELETE")
    public Result<ProcessGroup> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "procGroupCodes", required = true) String procGroupCodes) {
        String[] procGroupCode = procGroupCodes.split(",");
        for(String key : procGroupCode){
            Criteria criteria = Criteria.create();
            criteria.addCriterion("procGroupCode", Operator.EQ,key);
            lineService.removeBatch(ProcessGroupLine.class,criteria);
            service.delete(ProcessGroup.class, key);
        }
        return Result.OK();
    }

    @RequestMapping("save")
    @Operation(summary = "保存Bom", description = "保存Bom")
    public Result<ProcessGroup> save(@RequestBody ProcessGroup group) {
        service.save(group,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof ProcessGroupLine){
                    ((ProcessGroupLine) o).setProcGroupCode(group.getProcGroupCode());
                }
                return true;
            }
        });
        return Result.OK(group);
    }

    @GetMapping("unrelated/page/{productItemCode}")
    @Operation(summary = "产品工艺路线未匹配工序段列表", description = "获取产品工艺路线未匹配工序段列表", method = "GET")
    public Result<Page<ProcessGroup>> getUnrelatedProcGroupPage(@Parameter(description = "产品编码")  @PathVariable(name = "productItemCode", required = true) String productItemCode,
                                                         Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<ProcessGroup> page = service.getUnrelatedProcGroupPage(productItemCode, pageAble, criteria);
        return Result.OK(page);
    }

}
